const Controller = require("egg").Controller;

const cf_table = "runoob_tbl";

class CodeforcesController extends Controller {
  async select() {
    const { ctx, app } = this;
    const { SuccessModel, ErrorModel } = ctx.service.resModel;
    const params = ctx.request.body;
    params.datetime = new Date(params.datetime);
    await new Promise(async (resolve, reject) => {
      const result = await app.mysql.select(cf_table).catch((error) => {
        reject(error);
      });
      resolve(result);
    })
      .then((result) => {
        ctx.body = JSON.stringify(new SuccessModel(result, "查询数据成功！"));
      })
      .catch((error) => {
        ctx.body = JSON.stringify(new ErrorModel(error, "查询数据失败！"));
      });
  }
  async insert() {
    const { ctx, app } = this;
    const { SuccessModel, ErrorModel } = ctx.service.resModel;
    const params = ctx.request.body;
    params.datetime = new Date(params.datetime);
    await new Promise(async (resolve, reject) => {
      const result = await app.mysql.insert(cf_table, params).catch((error) => {
        reject(error);
      });
      resolve(result);
    })
      .then((result) => {
        ctx.body = JSON.stringify(new SuccessModel(result, "插入数据成功！"));
      })
      .catch((error) => {
        ctx.body = JSON.stringify(new ErrorModel(error, "插入数据失败！"));
      });
  }
  async update() {
    const { ctx, app } = this;
    const { SuccessModel, ErrorModel } = ctx.service.resModel;
    const { SubmissionId, ...row } = ctx.request.body;
    if (SubmissionId == null) {
      ctx.body = JSON.stringify(
        new ErrorModel(error, "更新数据失败，请求信息中找不到主键！")
      );
      return;
    }
    if (row.datetime != null) row.datetime = new Date(row.datetime);
    const options = {
      where: {
        SubmissionId,
      },
    };
    await new Promise(async (resolve, reject) => {
      const result = await app.mysql
        .update(cf_table, row, options)
        .catch((error) => {
          reject(error);
        });
      resolve(result);
    })
      .then((result) => {
        if (result.affectedRows === 0) {
          ctx.body = JSON.stringify(
            // affectedRows: 0,
            new ErrorModel(result, "更新数据失败，表中无对应行（主键）！")
          );
        } else {
          // affectedRows: 1,
          ctx.body = JSON.stringify(new SuccessModel(result, "更新数据成功！"));
          // message: '(Rows matched: 1  Changed: 1  Warnings: 0',  这种算是彻底的成功
          // message: '(Rows matched: 1  Changed: 0  Warnings: 0',  这种其实还是失败，但没有太大影响
        }
      })
      .catch((error) => {
        ctx.body = JSON.stringify(new ErrorModel(error, "更新数据失败！"));
      });
  }
  async delete() {
    const { ctx, app } = this;
    const { SuccessModel, ErrorModel } = ctx.service.resModel;
    const { SubmissionId } = ctx.request.body;
    if (SubmissionId == null) {
      ctx.body = JSON.stringify(
        new ErrorModel(error, "删除数据失败，请求信息中找不到主键！")
      );
      return;
    }
    const options = {
      SubmissionId,
    };
    await new Promise(async (resolve, reject) => {
      const result = await app.mysql
        .delete(cf_table, options)
        .catch((error) => {
          reject(error);
        });
      resolve(result);
    })
      .then((result) => {
        if (result.affectedRows === 0) {
          ctx.body = JSON.stringify(
            // affectedRows: 0,
            new ErrorModel(result, "删除数据失败，表中无对应行（主键）！")
          );
        } else {
          // affectedRows: 1,
          ctx.body = JSON.stringify(new SuccessModel(result, "删除数据成功！"));
        }
      })
      .catch((error) => {
        ctx.body = JSON.stringify(new ErrorModel(error, "删除数据失败！"));
      });
  }
}

module.exports = CodeforcesController;
